package com.starwish.starwishblog.web.front;

import com.starwish.starwishblog.entity.Tag;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.TagService;
import com.starwish.starwishblog.vo.TagVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/tag")
public class FTagController {

    @Autowired
    TagService tagService;

    @PostMapping("/modifyTag")
    public ResultResponse<Void> modify(@RequestBody Tag tag){
        ResultResponse<Void> result= null;

        try {
            Tag oldTag = tagService.findById(tag.getId());
            oldTag.setName(tag.getName());
            oldTag.setModifyTime(new Date());
            tagService.modifytag(oldTag);
            result = new ResultResponse<>(200,"修改成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改失败");
        }
        return  result;
    }

    @PostMapping("/addTag")
    public ResultResponse<Void> addTag(@RequestParam("articleId")Integer articleId,
                                       @RequestParam("userId")Integer userId,
                                       @RequestParam("tagName")String tagName){
        ResultResponse<Void> result = null;

        try {
            int i = tagService.addTagFront(articleId, userId, tagName);
            if(i > 0){
                result = new ResultResponse<>(200,"添加标签成功");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加标签失败!");
        }
        return result ;
    }


    @DeleteMapping("/deleteById/{id}")
    public ResultResponse<Void> delTagById(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = tagService.removeById(id);
            result = new ResultResponse<>(200,"删除标签成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除标签失败!");
        }
        return result ;
    }

    @GetMapping("/getHotTags")
    public ResultResponse<List<Tag>> getHotTags(){
        ResultResponse<List<Tag>> result = null;
        try {
            List<Tag> tagList = tagService.getHotTags();
            result = new ResultResponse<>(200,"查询最火标签成功!",tagList);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询最火标签失败!");
        }
        return result ;
    }

}
